<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册表单</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f9;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .error {
            color: red;
            font-size: 0.9em;
        }
    </style>
</head>

<body>
    <form id="registrationForm">
        <h2>用户注册</h2>
        <input type="text" id="username" placeholder="用户名">
        <div id="usernameError" class="error"></div>
        <input type="email" id="email" placeholder="邮箱">
        <div id="emailError" class="error"></div>
        <input type="password" id="password" placeholder="密码">
        <div id="passwordError" class="error"></div>
        <button type="submit">注册</button>
    </form>
    <script>
        const registrationForm = document.getElementById('registrationForm');
        const username = document.getElementById('username');
        const email = document.getElementById('email');
        const password = document.getElementById('password');
        const usernameError = document.getElementById('usernameError');
        const emailError = document.getElementById('emailError');
        const passwordError = document.getElementById('passwordError');

        registrationForm.addEventListener('submit', function (e) {
            e.preventDefault();
            let isValid = true;

            if (username.value.length < 3) {
                usernameError.textContent = '用户名必须至少3个字符！';
                isValid = false;
            } else {
                usernameError.textContent = '';
            }

            const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
            if (!emailRegex.test(email.value)) {
                emailError.textContent = '请输入有效的邮箱！';
                isValid = false;
            } else {
                emailError.textContent = '';
            }

            if (password.value.length < 6) {
                passwordError.textContent = '密码必须至少6个字符！';
                isValid = false;
            } else {
                passwordError.textContent = '';
            }

            if (isValid) {
                alert('注册成功！');
                registrationForm.reset();
            }
        });
    </script>
</body>

</html>
    